package com.zhanbo.zhanbowymusic.main.mine.recently;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.mwx.baselib.base.BaseBindingAdapter;
import com.mwx.baselib.base.BaseFragment;
import com.mwx.baselib.bean.SongBean;
import com.mwx.baselib.constant.Constant;
import com.mwx.baselib.utils.GlideUtils;
import com.mwx.baselib.utils.SDFileUtility;
import com.mwx.baselib.utils.SPUtils;
import com.zhanbo.zhanbowymusic.MyApplication;
import com.zhanbo.zhanbowymusic.R;
import com.zhanbo.zhanbowymusic.databinding.FragmentRecentlySingleSongBinding;
import com.zhanbo.zhanbowymusic.main.mine.recently.adapter.RecentlyPlayListAdapter;
import com.zhanbo.zhanbowymusic.music.MusicController;
import com.zhanbo.zhanbowymusic.music.OnEvent;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class SingleSongRecentlyFragment extends BaseFragment<FragmentRecentlySingleSongBinding> {
    private String detail;
    private RecentlyPlayListAdapter recentlyPlayListAdapter;

    @Override
    protected Object getLayout() {
        return R.layout.fragment_recently_single_song;
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        EventBus.getDefault().register(this);
        initView();
        initListener();
    }

    @Override
    public void onLazyInitView(@Nullable Bundle savedInstanceState) {
        super.onLazyInitView(savedInstanceState);
        initData();
    }

    @Subscribe
    public void getEvent(OnEvent event) {

        if (event.id==3) {
            initData();
            }
    }


    @Override
    public void onResume() {
        super.onResume();


    }

    @Override
    protected void initData() {
        SDFileUtility sdHelper2 = new SDFileUtility(MyApplication.getAppContext());
        try {

            detail = sdHelper2.readFromSD(Constant.RECENTLY_PLAYED);
            //添加到最近播放列表
            if (!TextUtils.isEmpty(detail)){
                List<SongBean> recentlyPlayedList =  new Gson().fromJson(detail,new TypeToken<List<SongBean>>() {}.getType());
                Collections.reverse(recentlyPlayedList);
                recentlyPlayListAdapter.getItems().clear();
                recentlyPlayListAdapter.getItems().addAll(recentlyPlayedList);
                recentlyPlayListAdapter.notifyDataSetChanged();
            }
        } catch (Exception e) {

        }
    }

    @Override
    protected void initView() {
        binding.recyclerSingleSong.setLayoutManager(new LinearLayoutManager(_mActivity,LinearLayoutManager.VERTICAL,false));
        recentlyPlayListAdapter = new RecentlyPlayListAdapter(_mActivity);
        binding.recyclerSingleSong.setAdapter(recentlyPlayListAdapter);
    }

    @Override
    protected void initListener() {
        recentlyPlayListAdapter.setOnItemClickListener(new BaseBindingAdapter.OnItemClickListener<SongBean>() {
            @Override
            public void onItemClick(int position) {
                MusicController.getInstance().updateItemDate(recentlyPlayListAdapter.getItems().get(position));
            }
        });
    }

    public static SingleSongRecentlyFragment newInstance() {

        Bundle args = new Bundle();

        SingleSongRecentlyFragment fragment = new SingleSongRecentlyFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    public void playAll() {
        if (recentlyPlayListAdapter.getItems()!=null){
            MusicController.getInstance().updateAndPlaySheetData(recentlyPlayListAdapter.getItems());
        }
    }
}
